
//
// Copyright 2009 Arne Halvorsen (arne.halvorsen@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Project page: http://code.google.com/p/mf4j/

//
// 091128 - aha42 - Created.
//

package org.aha.mf4j.visitor;

import java.io.CharArrayWriter;
import java.io.PrintWriter;

import org.aha.mf4j.response.FlickrObject;

/**
 * <p>
 *   {@link Visitor} similar to
 *   {@link PrintVisitor} but produces print as a 
 *   {@link String} that can be fetched from
 *   {@link #toString()} after traversal.
 * </p>
 * @author Arne Halvorsen (aha42)
 */
public final class ToStringVisitor extends Visitor 
{
  private final CharArrayWriter m_caw=new CharArrayWriter();
  
  private final PrintWriter m_pw=new PrintWriter(m_caw);
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ToStringVisitor(){}
  
  @Override
  public void start(){ m_caw.reset(); }
  
  @Override
  public void end(){ m_pw.flush(); }

  @Override
  public boolean visit(int level, FlickrObject visited) 
  {
    StringBuilder sb=new StringBuilder();
    for (int i=0; i<level; i++) sb.append(' ');
    sb.append(visited);
    m_pw.println(sb.toString());
    return true;
  }
  
  @Override
  public String toString(){ return m_caw.toString(); }
  
}
